﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Web;

using FRL.DebtChased.Extensions;
using FRL.DebtChased.Types;

namespace FRL.DebtChased.Repository.PartModels
{

	/// <summary>
	/// Declaration of HitListModel class
	/// </summary>
	public class HitListModel : BasePartModel
	{

		/////////////////////////////////////////////////////////
		// Nested classes

		public class Element
		{

			/////////////////////////////////////////////////////////
			// Attributes

			internal int m_iInvoiceCount;
			public string InvoiceCountAsText
			{
				get { return m_iInvoiceCount.ToString(); }
			}

			internal int m_iAccountId;
			public int AccountId
			{
				get { return m_iAccountId; }
			}

			internal string m_strDebtorName;
			public string DebtorName
			{
				get { return m_strDebtorName.DotDotDot(20); }
			}

			internal string m_strClientName;
			public string ClientName
			{
				get { return m_strClientName.DotDotDot(20); }
			}

			internal Money m_mnAmount;
			public string AmountAsText
			{
				get { return m_mnAmount.ToString(); }
			}

			internal Money m_mnCommission;
			public string CommissionAsText
			{
				get { return m_mnCommission.ToString(); }
			}

			/////////////////////////////////////////////////////////
			// Construction

			public Element()
			{

				// Initialize member variables

			}

		}

		/////////////////////////////////////////////////////////////
		// Attributes

		private List<Element> m_elements = new List<Element>();
		public List<Element> Elements
		{
			get { return m_elements; }
		}

		public Money m_mnTotal;
		public string TotalAsText
		{
			get { return m_mnTotal.ToString(); }
		}

		/////////////////////////////////////////////////////////////
		// Construction

		public HitListModel( DebtChasedEntities ent, int iUserId )
		{

			// Load the accounts
			var results = from a in ent.Accounts.Include("AccountInvoices")
						  join c in ent.Clients on a.ClientId equals c.ClientId
						  where ( (iUserId == 0) || (a.AssignedUserId == iUserId) || (a.AssignedManagerId == iUserId) )
						  select new { Account = a, ClientName = c.Name };
			foreach (var result in results)
			{

				// Add a new element
				Element el = new Element() {
					m_iAccountId = result.Account.AccountId,
					m_strDebtorName = result.Account.DebtorName,
					m_strClientName = result.ClientName,
					m_mnCommission = new Money(100, Money.eCurrency.Sterling)					
				};
				m_elements.Add(el);
				el.m_iInvoiceCount = result.Account.AccountInvoices.Count();

				// Add up the account invoices
				for( int i1=0; i1<result.Account.AccountInvoices.Count; i1++ )
				{
					AccountInvoice ai = result.Account.AccountInvoices.ElementAt(i1);
					if (i1 == 0)
						el.m_mnAmount = new Money(ai.ToMoneyDecimal());
					else
						el.m_mnAmount += ai.ToMoneyDecimal();
				}

			}

			// Recalc totals
			Recalc();

		}

		/////////////////////////////////////////////////////////////
		// Operations

		public void Recalc()
		{

			// If there are elements...
			if (m_elements.Count > 0)
			{

				// Loop through elements
				for (int i1 = 0; i1 < m_elements.Count; i1++)
				{
					Element el = m_elements[i1];
					if (i1 == 0)
						m_mnTotal = new Money(el.m_mnCommission);
					else
						m_mnTotal += el.m_mnCommission;
				}

			}

		}

	}

}